package com.d5xx.hunters;

import com.d5xx.hunters.API.ConsoleColor;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import static com.d5xx.hunters.Variables.*;

public interface Files {
    static void folders() {
        //插件文件夹
        if (!dataFolder.exists() && !dataFolder.mkdir()) {
            plugin.getLogger().severe(ConsoleColor.red() + "Fail to create plugin folder, disabling plugin!" + ConsoleColor.reset());
            plugin.getPluginLoader().disablePlugin(plugin);
        }
        //语言文件夹
        if (!langFolder.exists() && !langFolder.mkdir()) {
            plugin.getLogger().severe(ConsoleColor.red() + "Fail to create plugin folder, disabling plugin!" + ConsoleColor.reset());
            plugin.getPluginLoader().disablePlugin(plugin);
        }
    }

    static void config() {
        //config.yml
        File configFile = new File(dataFolder, "config.yml");
        try {
            if (!configFile.exists() && !configFile.createNewFile()) {
                plugin.getLogger().severe(ConsoleColor.red() + "Fail to create config file, disabling plugin!" + ConsoleColor.reset());
                plugin.getPluginLoader().disablePlugin(plugin);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        config = YamlConfiguration.loadConfiguration(configFile);
        config.options().setHeader(new ArrayList<>(Arrays.asList(
                "此为Hunters插件的配置文件",
                "!!!请小心修改!!!"
        )));
        config.addDefault("settings.default-language", "zh-cn");
        config.addDefault("players.default-settings", new HashMap<>() {{
            put("language", "zh-cn");
        }});
        config.addDefault("settings.worldlist-size", 2);
        config.addDefault("settings.plugin-reload-colddown", 10);
        config.options().copyDefaults(true);
        try {
            config.save(configFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        config = YamlConfiguration.loadConfiguration(new File(dataFolder, "config.yml"));
    }

    static void langFile() {
        //zh-cn.yml
        File zhcnFile = new File(langFolder, "zh-cn.yml");
        try {
            if (!zhcnFile.exists() && !zhcnFile.createNewFile()) {
                plugin.getLogger().severe(ConsoleColor.red() + "Fail to create language file, disabling plugin!" + ConsoleColor.reset());
                plugin.getPluginLoader().disablePlugin(plugin);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        YamlConfiguration zhcn = YamlConfiguration.loadConfiguration(zhcnFile);
        zhcn.addDefault("lang-display-name", "简体中文");
        zhcn.addDefault("plugin-prefix", "§8[§b§lHunter§r§8] §r");
        zhcn.addDefault("HunterAdmin-help",
                """
                        §7======§cHunterAdmin帮助§7======
                        
                        §f§lhelp §7--显示此信息
                        §f§lsetlobby §7--设置大厅
                        
                        ========================"""
        );
        zhcn.addDefault("HunterAdmin-setlobby", "§a§l大厅设置成功！");
        zhcn.addDefault("Language-set","§l语言更改成功！当前语言为§e§l简体中文。");
        zhcn.addDefault("Language-help","§e§l用法：§r/language <your language>");
        zhcn.addDefault("player-kick-reload", "§a插件正在重载中...");
        zhcn.addDefault("player-kick-createMap", "§e服务器正在配置地图！请稍后再加入！");
        zhcn.addDefault("HunterPlayer-start-error", "§c游戏地图不足！请稍后再试。");
        zhcn.addDefault("HunterPlayer-waiting-error", "§c你已经在游戏中了!");
        zhcn.addDefault("player-waiting-actionbar", "§e匹配中");
        zhcn.addDefault("player-waiting-finish-actionbar", "§e游戏即将开始！");
        zhcn.addDefault("game-pvp-enable", "§cPVP已开启！");
        zhcn.addDefault("game-death-title", "§c你死了！");
        zhcn.addDefault("game-death-subtitle", "§7游戏结束，§e%s%§7秒后返回大厅");
        zhcn.addDefault("game-respawn-title", "§a已重生！");
        zhcn.addDefault("game-win-title", "§6§l胜利！");
        zhcn.addDefault("game-scoreboard-player-type", "§f角色：§7%type%");
        zhcn.addDefault("game-scoreboard-game-type", "§f模式：§7%type%");
        zhcn.addDefault("server-ip", "§bleo-mc.tpddns.cn");
        zhcn.addDefault("game-help",
                """
                        §f======§e§lHUNTER GAME§f======
                        
                          §f你的角色：%player-type%
                          §f目标：§7%player-target%
                          §7游戏开始后你将有§c10§7秒免伤时间，
                          §7请快速跳下出发台并开始游戏！
                        
                        §f========================"""
        );
        zhcn.addDefault("game-start-type-escapee", "§b逃生者");
        zhcn.addDefault("game-start-target-escapee", "在猎人的追杀下通关游戏！");
        zhcn.addDefault("game-start-type-hunter", "§6猎人");
        zhcn.addDefault("game-start-target-hunter", "§7阻止逃生者通关游戏！");
        zhcn.addDefault("cmd-admin-move", "§c找不到该世界！");
        //zhcn.addDefault("scoreboard-title-colors", new ArrayList<>(Arrays.asList(""))); //看lobbyscoreboard的prefix
        zhcn.options().copyDefaults(true);
        try {
            zhcn.save(zhcnFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    static void storageFile() {
        //storage.yml
        File storageFile = new File(dataFolder, "storage.yml");
        try {
            if (!storageFile.exists() && !storageFile.createNewFile()) {
                plugin.getLogger().severe(ConsoleColor.red() + "Fail to create storage file, disabling plugin!" + ConsoleColor.reset());
                plugin.getPluginLoader().disablePlugin(plugin);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        YamlConfiguration storage = YamlConfiguration.loadConfiguration(storageFile);
        storage.addDefault("lobby.world", "");
        storage.addDefault("lobby.x", "");
        storage.addDefault("lobby.y", "");
        storage.addDefault("lobby.z", "");
        storage.addDefault("lobby.yaw", "");
        storage.addDefault("lobby.pitch", "");
        storage.addDefault("players", "");
        storage.options().copyDefaults(true);
        try {
            storage.save(storageFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
